package com.example.suze.libraryinformationmanagementsystem.Activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.suze.libraryinformationmanagementsystem.R;
import com.example.suze.libraryinformationmanagementsystem.net.API;
import com.example.suze.libraryinformationmanagementsystem.net.JSONCallBack;
import com.example.suze.libraryinformationmanagementsystem.net.entiry.BookRoot;
import com.example.suze.libraryinformationmanagementsystem.net.entiry.ReturnStateEntity;
import com.person.CommonUtils;
import com.person.dao.BookDao;
import com.person.dao.DBManager;
import com.person.entity.Book;
import com.person.entity.Notes;

import java.util.List;

/**
 * Created by suze on 2017/6/12.
 */

public class LentActivity extends BaseActivity {
    private EditText book_id,person_id,time_in;
    private Button sure;
    private JSONCallBack callBack,callBack1,callBack2;
    private String book_name;
    API api = API.getAPI(LentActivity.this);
    private String book_id1,person_id1,time_in1;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lent_book);
        final CommonUtils commonUtils = new CommonUtils(this);
        book_id = (EditText) findViewById(R.id.ev_book_lent_id);
        person_id = (EditText) findViewById(R.id.ev_book_lent_student_id);
        time_in = (EditText) findViewById(R.id.ev_book_lent_time_in);
        sure = (Button) findViewById(R.id.bt_book_lent_sure);
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                book_id1 =book_id.getText().toString();
                person_id1 = person_id.getText().toString();
                time_in1 = time_in.getText().toString();
                callBack = new JSONCallBack<BookRoot>(new BookRoot()) {
                    @Override
                    public void onSuccess(BookRoot entity) {
                        if(entity.getstate().equals("success") && entity.getcode() == 0){
                            if(entity.getdata().size()>0){
                                book_name = entity.getdata().get(0).getbookname();
                                if(entity.getdata().get(0).getis_lend()){
                                    Looper.prepare();
                                    Toast.makeText(LentActivity.this, "这本书已经借出去了", Toast.LENGTH_LONG).show();
                                    Looper.loop();
                                }else {
                                    callBack1 = new JSONCallBack<ReturnStateEntity>(new ReturnStateEntity()) {
                                        @Override
                                        public void onSuccess(ReturnStateEntity entity) {
                                            Log.v("login", entity.getState()+" "+entity.getCode());
                                            if(entity.getState().equals("success") && entity.getCode() == 0){
                                                callBack2 = new JSONCallBack<ReturnStateEntity>(new ReturnStateEntity()) {
                                                    @Override
                                                    public void onSuccess(ReturnStateEntity entity) {
                                                        Log.v("login", entity.getState()+" "+entity.getCode());
                                                        if(entity.getState().equals("success") && entity.getCode() == 0){
                                                            Looper.prepare();
                                                            Toast.makeText(LentActivity.this, "借书成功", Toast.LENGTH_LONG).show();
                                                            finish();
                                                            Looper.loop();
                                                            Toast.makeText(LentActivity.this, "借书成功", Toast.LENGTH_LONG).show();
                                                        }else{
                                                            Looper.prepare();
                                                            Toast.makeText(LentActivity.this, "插入借阅信息错误", Toast.LENGTH_LONG).show();
                                                            Looper.loop();
                                                        }
                                                    }
                                                };
                                              api.sqlOporate("insert","INSERT INTO borrow_information ( No, reader_name,book_name,borrow_date,return_date,status)VALUES('"+book_id1+"','吴彦祖','"+book_name+"','2017年6月21日','"+time_in1+"',1 )",callBack2);

                                            }else{
                                                Looper.prepare();
                                                Toast.makeText(LentActivity.this, "修改借出状态失败", Toast.LENGTH_LONG).show();
                                                Looper.loop();
                                            }
                                        }
                                    };
                                    api.sqlOporate("update", "UPDATE book set is_lend = 1 WHERE NO="+"'"+book_id1+"'", callBack1);

                                }
                            }else {
                                Looper.prepare();
                                Toast.makeText(LentActivity.this, "这本书不存在", Toast.LENGTH_LONG).show();
                                Looper.loop();
                            }

                        }else{
                            Looper.prepare();
                            Toast.makeText(LentActivity.this, "请求失败", Toast.LENGTH_LONG).show();
                            Looper.loop();
                        }
                    }
                };
                api.sqlOporate("select", "SELECT * FROM book WHERE NO="+"'"+book_id1+"'", callBack);
//                DBManager dbManager = new DBManager(LentActivity.this);
//                BookDao bookDao = dbManager.getDaoSession().getBookDao();
//                List<Book> books = bookDao.queryBuilder().where(BookDao.Properties.Id.eq(book_id1)).build().list();
//                if(books.size()>0){
//                    int status =books.get(0).getStatus();
//                    if(status==0){
//                        bookDao.deleteByKey(book_id1);
//                        books.get(0).setStatus(1);
//                        books.get(0).setTimeOut("2017年6月17日");
//                        books.get(0).setBorrowerId(person_id1);
//                        books.get(0).setTimeIn(time_in1);
//                        commonUtils.insertFriend(books.get(0));
//                        String book_name = books.get(0).getName();
//                        String time_out = "2017年6月17日";
//                        int status1 = 0;
//                        Notes notes = new Notes();
//                        notes.setBookId(book_id1);
//                        notes.setBookName(book_name);
//                        notes.setPersonId(person_id1);
//                        notes.setTimeOut(time_out);
//                        notes.setTimeIn(time_in1);
//                        notes.setStatus(status1);
//                        commonUtils.insertNotes(notes);
//                        finish();
//                    }else {
//                        Toast.makeText(LentActivity.this,"这本书已经借出去了~",Toast.LENGTH_SHORT).show();
//                    }
//                }else {
//                    Toast.makeText(LentActivity.this,"这本书不存在~",Toast.LENGTH_SHORT).show();
//                }


            }
        });
    }
}
